package org.vaadin.vaadinjit.widgetset.client.ui;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.json.client.JSONObject;


/**
 * This establishes an overlay of the JIT Canvas class, which is used in JIT to handle
 * size of the visualization and more generic DOM related properties.
 * 
 * @author jonkka
 *
 */
public final class JitCanvasOverlay extends JitOverlay {

	protected JitCanvasOverlay() {}

	public static final int WIDTH = 0;
	
	public static final int HEIGHT = 1;
	
	private static final String JSON_WIDTH = "width";
	
	private static final String JSON_HEIGHT = "height";

	
	/**
	 * Used to resize the native canvas of the visualization. the parameters should be
	 * given in pixels.
	 * 
	 * @param width the desired width in pixels
	 * @param height the desired height in pixels
	 */
	public native void resize(int width, int height) /*-{
		this.resize(width, height);
	}-*/;
	
	
	/**
	 * Get the width and height of the native canvas element. This returns an array
	 * of size 2, with the width in index 0 and height in index 1. The sizes are given
	 * in pixels.
	 * 
	 * @return an array containing the width and height of the canvas
	 */
	public Integer[] getSize() {
		JSONObject jsonSize = new JSONObject(getNativeSize());			
		
		Integer width = (int) jsonSize.get(JSON_WIDTH).isNumber().doubleValue();
		Integer height = (int) jsonSize.get(JSON_HEIGHT).isNumber().doubleValue();
		
		Integer[] sizeArray = {width, height};
		
		return sizeArray;
	}
	
	
	/**
	 * Private method to natively get the size. This returns width and height as
	 * JavaScript object which can be interpreted as JSON data.
	 * 
	 * @return a JavaScript object containing width and height
	 */
	private native JavaScriptObject getNativeSize() /*-{
		return this.getSize();
	}-*/;
	
}
